package sv.mecca.modelo;

import java.sql.SQLException;
import java.util.ArrayList;

import sv.gob.hnr.modelo.Conexion;
import sv.mecca.form.LupusForm;

public class OperacionesLupus extends Conexion{

    public void insertarLupus(LupusForm l) throws SQLException{
        try {
            st = getConexion().createStatement();
            st.execute("insert into lupus(expediente_paciente, fecha_diagnostico, patologias, complicaciones, talla, peso) values(" +
                "'" + l.getExpediente() + "', " +
                "'" + l.getFecha() + "', " +
                "'" + l.getPatologias()+ "', " +
                "'" + l.getComplicaciones() + "', " +
                "" + l.getTalla() + ", " +
                "" + l.getPeso() + ")");
        } catch (Exception e) {
            System.out.println(e.getMessage());
            }
        finally{
            st.close();
            cn.close();
            }
        l.destroy();
        }
    public LupusForm modificarLupus(Integer id) throws SQLException{
        LupusForm l= new LupusForm();
        try {
            st = getConexion().createStatement();
            rs = st.executeQuery("select * from lupus where id = " + id);
            rs.next();
            l.setId(rs.getInt(1));
            l.setExpediente(rs.getString(2));
            l.setFecha(rs.getString(3));
            l.setPatologias(rs.getString(4));
            l.setComplicaciones(rs.getString(5));
            l.setTalla(rs.getDouble(6));
            l.setPeso(rs.getDouble(7));
        } catch (Exception e) {
            System.out.println(e.getMessage());
            }
        finally{
            rs.close();
            st.close();
            cn.close();
            return l;
            }
        }
    public void actualizarLupus(LupusForm l, Integer id) throws SQLException{
        try {
            st = getConexion().createStatement();
            st.executeUpdate("update lupus set " +
                "expediente_paciente = '" + l.getExpediente() + "', " +
                "fecha_diagnostico = '" + l.getFecha() + "', " +
                "patologias = '" + l.getPatologias() + "', " +
                "complicaciones = '" + l.getComplicaciones() + "', " +
                "talla = " + l.getTalla() + ", " +
                "peso = " + l.getPeso() + " " +
                "where id = " + id);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            }
        finally{
            st.close();
            cn.close();
            }
        l.destroy();
        }
    public void EliminarLupus(Integer id) throws SQLException{
        try {
            st = getConexion().createStatement();
            st.execute("delete from lupus where id = " + id);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            }
        finally{
            st.close();
            cn.close();
            }
        }
    public ArrayList<LupusForm> listado() throws SQLException{
        ArrayList<LupusForm> lupus = null;
        try {
            st = getConexion().createStatement();
            rs = st.executeQuery("select * from lupus");
            lupus = new ArrayList<LupusForm>();
            while (rs.next()) {
                LupusForm l = new LupusForm(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getDouble(6), rs.getDouble(7));
                lupus.add(l);
                }
            rs.close();
        } catch (Exception e) {
            System.out.println(e.getMessage());
            }
        finally{
            st.close();
            cn.close();
            return lupus;
            }
        }
    }